package com.treeengine.game.camera;

import javax.microedition.khronos.opengles.GL10;

import com.treeengine.game.graphics.Graphics;
import com.treeengine.game.math.Vector2;

public class Camera2D implements ICamera {
	
    public Vector2 position;
    public float zoom;
    
    protected float frustumWidth;
    protected float frustumHeight;
    protected Graphics graphics;

    public Camera2D(Graphics graphics, float frustumWidth, float frustumHeight) {
        this.graphics = graphics;
        
        this.frustumWidth = frustumWidth;
        this.frustumHeight = frustumHeight;
        
        this.position = new Vector2(frustumWidth / 2, frustumHeight / 2);
        
        this.zoom = 1.0f;
    }
    
    public Camera2D(Graphics graphics, float frustumWidth, float frustumHeight, float initX, float initY) {
        this.graphics = graphics;
        
        this.frustumWidth = frustumWidth;
        this.frustumHeight = frustumHeight;
        
        this.position = new Vector2(initX, initY);
        
        this.zoom = 1.0f;
    }
    
    public Camera2D(Graphics graphics, float frustumWidth, float frustumHeight, float zoom) {
        this(graphics, frustumWidth, frustumHeight);
        
        this.zoom = zoom;
    }
    
    public Camera2D(Graphics graphics, float frustumWidth, float frustumHeight, float initX, float initY, float zoom) {
        this(graphics, frustumWidth,frustumHeight,initX,initY);
        
        this.zoom =zoom;
    }
    
    

    public void setViewportAndMatrices() {
        GL10 gl = graphics.getGL();
        gl.glViewport(0, 0, graphics.getWidth(), graphics.getHeight());
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(position.getX() - frustumWidth * zoom / 2, 
                    position.getX() + frustumWidth * zoom/ 2, 
                    position.getY() - frustumHeight * zoom / 2, 
                    position.getY() + frustumHeight * zoom/ 2, 
                    1, -1);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    /**
     * ? 
     * @param touch
     */
    public void touchToWorld(Vector2 touch) {
       /* touch.x = (touch.x / (float) graphics.getWidth()) * frustumWidth * zoom;
        touch.y = (1 - touch.y / (float) graphics.getHeight()) * frustumHeight * zoom;
        touch.add(position).sub(frustumWidth * zoom / 2, frustumHeight * zoom / 2);*/
    }
}